package com.slipper.codeless.controller;

import com.slipper.core.tree.TreeInfo;
import com.slipper.core.tree.TreeUtil;
import com.slipper.system.area.bo.AreaBO;
import com.slipper.system.area.service.AreaService;
import com.slipper.util.BeanUtil;
import com.slipper.util.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/area")
public class AreaApiController {
    @Resource
    private AreaService areaService;

    @PostMapping("/queryBy")
    public String queryBy(AreaBO bo) {
        return ResultUtil.successResult(this.areaService.queryBy(bo.getId()));
    }

    @PostMapping("/list")
    public String queryList() {
        return ResultUtil.successResult(this.areaService.queryList(new AreaBO()));
    }

    @PostMapping("/tree")
    public String queryTree() {
        List<AreaBO> areaList = areaService.queryList(new AreaBO());
        TreeInfo treeInfo = new TreeInfo("区域树");
        treeInfo.setRootId("000");
        treeInfo.setIdField("id");
        treeInfo.setPidField("pid");
        treeInfo.setTextField("name");
        treeInfo.setSortField("ordinal");
        return ResultUtil.successResult(TreeUtil.buildTree(treeInfo, BeanUtil.setOrdinal(areaList), "asc"));
    }
}
